package org.example.core.handler;

import lombok.extern.slf4j.Slf4j;
import org.example.core.exception.CustomException;
import org.example.core.utils.ResultUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.example.core.model.entity.CustomResult;
/**
 * @author:hzc
 * @date:2024/7/8
 * @desc: 全局的的异常拦截器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 自定义异常拦截器
     * @param e
     * @return
     */
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public CustomResult errorHandler(CustomException e){
        return ResultUtils.fail(e.getCode(),e.getMsg());
    }
    @ExceptionHandler(Exception.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    public CustomResult errorHandler(Exception e){
        return ResultUtils.fail(500,e.getMessage());
    }
}
